<template>
  <!--合作伙伴-->
  <div class="statistics-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="8">
        <el-button type="primary" @click="add">添加</el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="16">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.keyword"
              style="margin-right: 10px"
              placeholder="请输入标题"
              clearable
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="search">
              查询
            </el-button>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-refresh-right"
              @click="restart"
            >
              重置
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      @selection-change="setSelectRows"
    >
      <el-table-column
        align="center"
        show-overflow-tooltip
        label="序号"
        sortable
        type="index"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="title"
        label="名称"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="order"
        label="排序值"
      />
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="edit(scope)">编辑</el-button>
          <el-button type="text" @click="delList(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加编辑 -->
    <el-dialog :title="wintit" :visible.sync="dialogFormVisible" width="900px">
      <el-form :model="form" :inline="true" label-width="100px">
        <el-form-item label="标题:">
          <el-input v-model="form.title" placeholder="请输入标题"  />
        </el-form-item>
        <el-form-item label="排序值:">
          <el-input
            v-model="form.order"
            placeholder="请输入排序值,越大越在前面"
          />
        </el-form-item>
        <el-form-item label="跳转地址:" style="width: 100%">
          <el-input
            v-model="form.skip_url"
            placeholder="请输入跳转连接"
            style="width: 100%;"
          />
        </el-form-item>
        <el-form-item label="上传封面:" style="width: 100%">
          <el-upload
            class="uploads"
            ref="upload_url"
            action=""
            list-type="picture-card"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove"
            :http-request="uploadImg"
            :limit="1"
          >
            <img v-if="is_edit_url && img != ''" width="148px" height="148px" style="overflow: hidden"  :src="img" class="avatar" /> <!--:show-file-list="false"-->
            <i class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
<!--        <el-form-item label="上传附件:" style="width: 30%">
          <el-upload
            class="upload-demo"
            action=""
            list-type="picture-card"
            :show-file-list="false"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove"
            :http-request="uploadFile"
          >
&lt;!&ndash;            <img v-if="files" width="150px" :src="files" class="avatar" />&ndash;&gt;
            <i class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>-->
        <el-form-item label="上传附件:" style="width: 100%">
          <el-button type="primary" @click="uploadfile()">
            上传文件
          </el-button>
        </el-form-item>

        <el-form-item label=" " style="width: 100%">
          <el-table
            :data="tableData"
            style="width: 100%">
            <el-table-column
              prop="file_name"
              align="center"
              label="文件名"
              width="140" />
            <el-table-column
              align="center"
              label="图标"
              width="100" >
              <template #default="{ row }">
                <img width="50px" height="50px" style="overflow: hidden" :src="row.file_icon" class="avatar" />
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              width="200"
              label="操作">
              <template #default="{ row,$index }">
                <el-button type="primary" @click="uploadedit(row,$index)">
                  编辑
                </el-button>
                <el-button type="danger" @click="uploaddelete($index)">
                  删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
        <el-form-item label="填写内容:" style="width: 100%">
          <quill-editor
            ref="text"
            v-model="form.content"
            class="myQuillEditor"
            style="height:200px;margin-bottom: 40px"
            :options="editorOption"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button v-show="addtype" type="primary" @click="send()">
          确 定
        </el-button>
        <el-button v-show="edittype" type="primary" @click="save()">
          确 定
        </el-button>
      </div>
    </el-dialog>
    <!-- 上传文件 -->
    <el-dialog :title="wintit" :visible.sync="dialogFormVisible2" width="700px">
      <el-form :model="file" label-width="100px">
        <el-form-item label="文件展示名:">
          <el-input v-model="file.file_name" placeholder="请输入文件展示名"  />
        </el-form-item>
        <el-form-item label="上传图标:">
          <el-upload
            class="uploads"
            action=""
            ref="upload_icon"
            list-type="picture-card"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove2"
            :http-request="uploadIcon"
            :limit="1"
          >
            <img v-if="is_edit_icon && file.file_icon != ''" width="148px" height="148px" style="overflow: hidden" :src="file.file_icon" class="avatar" />
            <i  v-if="!is_edit_icon" class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>

        <el-form-item label="上传文件:">
          <el-upload
            class="uploads"
            action=""
            ref="upload_file"
            list-type="fileList"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove3"
            :http-request="uploadFile"
            :limit="1"
          >
            <el-button  size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label=" ">
          <div v-if="is_edit_file && file.file != ''">{{file.file}}</div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible2 = false">取 消</el-button>
        <el-button v-if="is_edit2" type="primary" @click="save3()">
          确 定
        </el-button>
        <el-button v-else-if="!is_edit2"  type="primary" @click="save2()">
          确 定
        </el-button>
      </div>
    </el-dialog>

    <el-pagination
      background
      :current-page="queryForm.page"
      :page-size="queryForm.list_row"
      :layout="layout"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
  import {
    getCooperationLists,
    addCooperations,
    delCooperations,
    editCooperations,
    getOssUploadsParam,
  } from '@/api/common'
  import axios from 'axios'
  import { quillEditor } from 'vue-quill-editor'
  import 'quill/dist/quill.core.css'
  import 'quill/dist/quill.snow.css'
  import 'quill/dist/quill.bubble.css'

  export default {
    name: 'Cooperation',
    components: { quillEditor },
    data() {
      return {
        user: [],
        queryForm: {
          page: 1,
          list_row: 10,
          keyword: '',
        },
        form: {
          title: '',
          order: '',
          skip_url: '',
          content: '',
        },
        disabled: false,
        listLoading: false,
        editorOption: { },
        list: [],
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        dialogFormVisible: false,
        dialogFormVisible2: false,
        wintit: '',
        coId: '',
        img: '',
        files: '',
        addtype: false,
        edittype: false,
        tableData:[],
        file:{
          file_name:'',
          file_icon:'',
          file:'',
        },
        is_edit:false,
        is_edit2:false, //false-新增文件 true-编辑文件
        sel_index:0,
        is_edit_url:false,
        is_edit_icon:false,
        is_edit_file:false,
      }
    },
    created() {
      this.getData()
    },
    methods: {
      //编辑上传文件
      uploadedit(row,index){
        this.sel_index = index;
        this.file.file_name = row.file_name;
        this.file.file_icon = row.file_icon;
        this.file.file = row.file;
        this.dialogFormVisible2 = true;
        this.is_edit2 = true;
        this.is_edit_icon = true
        this.is_edit_file = true
      },
      //删除文件
      uploaddelete(index){
        console.log(4,index)
        let data = this.tableData;
        delete  data[index]
        this.tableData = data.filter(Boolean);
      },
      //上传文件
      uploadfile(){
        this.file.file_name = '';
        this.file.file_icon = '';
        this.file.file = '';
        this.is_edit2 = false;
        this.dialogFormVisible2 = true;
      },
      //保存上传文件
      save2(){
        let resdata = JSON.parse(JSON.stringify(this.file))
        if(resdata.file_name === ''){
          this.$message.error('请输入文件展示名！')
          return false
        }
        if(resdata.file === ''){
          this.$message.error('请上传文件！')
          return false
        }
        this.tableData.push(resdata);
        this.dialogFormVisible2 = false;
        this.$refs.upload_icon.clearFiles();
        this.$refs.upload_file.clearFiles();
        this.$forceUpdate();
      },
      save3(){
        console.log(1,this.file)
        let resdata = this.file;
        if(resdata.file_name === ''){
          this.$message.error('请输入文件展示名！')
          return false
        }
        if(resdata.file === ''){
          this.$message.error('请上传文件！')
          return false
        }
        this.tableData[this.sel_index].file_name = resdata.file_name;
        this.tableData[this.sel_index].file = resdata.file;
        this.tableData[this.sel_index].file_icon = resdata.file_icon;
        console.log(11,this.tableData)
        this.dialogFormVisible2 = false;
        this.$refs.upload_icon.clearFiles();
        this.$refs.upload_file.clearFiles();
      },
      handleRemove(){},
      handlePictureCardPreview(){},
      handleDetail() {},
      //搜索
      search() {
        this.getData()
      },
      restart() {
        this.queryForm = {}
        this.getData()
      },
      getData() {
        let params = {
          page: this.queryForm.page,
          limit: this.queryForm.list_row,
          input: this.queryForm.keyword,
        }
        getCooperationLists(params).then((data) => {
          this.list = data.data
          this.total = data.count
        })
      },
      add() {
        this.dialogFormVisible = true
        this.wintit = '添加'
        this.addtype = true
        this.edittype = false
        this.is_edit = false;
        this.form = {}
        this.tableData = []
        this.img = ''
      },
      edit(row) {
        this.dialogFormVisible = true
        this.wintit = '编辑'
        this.addtype = false
        this.edittype = true
        this.is_edit = true;
        this.is_edit_url = true
        this.form = this.list[row.$index]
        this.img = this.list[row.$index].url
        this.tableData = this.list[row.$index].file_arr
        this.coId = row.row.id
      },
      send() {
        let params = {
          title: this.form.title,
          order: this.form.order,
          file_arr:JSON.stringify(this.tableData),
          skip_url: this.form.skip_url,
          content: this.form.content,
          url: this.img,
        }
        addCooperations(params).then((data) => {
          this.$message.success('添加成功！')
          this.dialogFormVisible = false
          this.getData()
        })
        this.$refs.upload_url.clearFiles();
      },
      save() {
        let params = {
          id: this.coId,
          title: this.form.title,
          order: this.form.order,
          file_arr:JSON.stringify(this.tableData),
          skip_url: this.form.skip_url,
          content: this.form.content,
          url: this.img,
        }
        editCooperations(params).then((data) => {
          this.$message.success('修改成功！')
          this.dialogFormVisible = false
          this.getData()
        })
        this.$refs.upload_url.clearFiles();
      },
      delList(row) {
        let params = {
          id: row.id,
        }
        delCooperations(params).then((data) => {
          this.$message.success('删除成功！')
          this.getData()
        })
      },
      uploadImg(params) {
        this.is_edit_url = false;
        console.log(123, params)
        const file = params.file,
          fileType = file.type,
          isImage = fileType.indexOf('image') != -1,
          isLt2M = file.size / 1024 / 1024 < 2
        // 这里常规检验，看项目需求而定
        if (!isImage) {
          this.$message.error('只能上传图片格式png、jpg、gif!')
          return
        }
        if (!isLt2M) {
          this.$message.error('只能上传图片大小小于2M')
          return
        }
        getOssUploadsParam().then((res) => {
          const form = new FormData()
          form.append('key', res.data.dir + file.name)
          form.append('OSSAccessKeyId', res.data.accessid)
          form.append('policy', res.data.policy)
          form.append('success_action_status', '200')
          form.append('signature', res.data.signature)
          form.append('file', file)
          console.log(222, res)
          axios({
            url: res.data.host,
            method: 'POST',
            data: form,
            processData: false,
            cache: false,
            contentType: false,
          }).then((data) => {
            this.img = res.data.host + '/' + res.data.dir + file.name
            console.log(66, this.img)
          })
        })
      },
      handleRemove2(){
        this.file.file_icon = '';
      },
      //上传图标
      uploadIcon(params) {
        console.log(123, params)
        this.is_edit_icon = false;
        const file = params.file,
          fileType = file.type,
          isImage = fileType.indexOf('image') != -1,
          isLt2M = file.size / 1024 / 1024 < 2
        // 这里常规检验，看项目需求而定
        if (!isImage) {
          this.$message.error('只能上传图片格式png、jpg、gif!')
          return
        }
        if (!isLt2M) {
          this.$message.error('只能上传图片大小小于2M')
          return
        }
        getOssUploadsParam().then((res) => {
          const form = new FormData()
          form.append('key', res.data.dir + file.name)
          form.append('OSSAccessKeyId', res.data.accessid)
          form.append('policy', res.data.policy)
          form.append('success_action_status', '200')
          form.append('signature', res.data.signature)
          form.append('file', file)
          console.log(222, res)
          axios({
            url: res.data.host,
            method: 'POST',
            data: form,
            processData: false,
            cache: false,
            contentType: false,
          }).then((data) => {
            this.file.file_icon = res.data.host + '/' + res.data.dir + file.name
          })
        })
      },
      handleRemove3(){
        this.file.file = '';
      },
      uploadFile(params) {
        this.is_edit_file = false
        const file = params.file,
          fileType = file.type,
          isImage = fileType.indexOf('image') != -1,
          isLt2M = file.size / 1024 / 1024 < 2
        getOssUploadsParam().then((res) => {
          const form = new FormData()
          form.append('key', res.data.dir + file.name)
          form.append('OSSAccessKeyId', res.data.accessid)
          form.append('policy', res.data.policy)
          form.append('success_action_status', '200')
          form.append('signature', res.data.signature)
          form.append('file', file)
          axios({
            url: res.data.host,
            method: 'POST',
            data: form,
            processData: false,
            cache: false,
            contentType: false,
          }).then((data) => {
            this.file.file = res.data.host + '/' + res.data.dir + file.name
            console.log(66, this.file.file)
          })
        })
      },
      setSelectRows() {},
      handleSizeChange(val) {
        this.queryForm.list_row = val
        this.getData()
      },
      handleCurrentChange(val) {
        this.queryForm.page = val
        this.getData()
      },
      selectType(e) {
        this.queryForm.types === e.value
      },
    },
  }
</script>

<style lang="scss" scoped>
  .el-button {
    font-size: 14px;
  }
</style>
